//
//  SEGExpressCollectionDetailViewModel.m
//  SEGViewModel
//
//  Created by 乔杨 on 2020/6/1.
//

#import "SEGExpressCollectionDetailViewModel.h"
#import "SEGExpressRequestModel.h"

@implementation SEGExpressCollectionDetailViewModel

- (instancetype)init {
    self = [super init];
    if (self) {
        self.rowNames = @[@[@"状态", @"快递单号", @"提取码", @"柜号"], @[@"快递公司", @"收件人", @"联系电话", @"地址", @"到达时间"]];
        [self createSingal];
    }
    return self;
}
- (void)createSingal {
    @weakify(self);
    self.command = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(NSString *expressSid) {
        return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
           @strongify(self);
            [[SEGExpressRequestModel sharedRequestModel] requestReceiveExpressDetailWithExpressSid:expressSid finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
                if (isSuccessed) {
                    SEGExpressModel *model = [SEGExpressModel yy_modelWithDictionary:[result objectForKey:@"data"]];
                    self.expressModel = model;
                    self.dataSource = @[@[model.expressStatus ?: @"",
                                              model.expressCode ?: @"",
                                              model.receiveKey ?: @"",
                                              [NSString stringWithFormat:@"%@", [model.assignedCode length] > 0 ? model.assignedCode : @"-"], 
                                              ],
                                            @[model.expressCompanyName ?: @"",
                                              model.expressReceiveName ?: @"",
                                              model.expressReceiveTel ?: @"",
                                              model.expressReceiveAddress ?: @"",
                                              model.expressReceiveTime ?: @""]];
                    
                    [subscriber sendNext:nil];
                    [subscriber sendCompleted];
                } else {
                    NSError *error = [NSError errorWithDomain:@"" code:0 userInfo:@{NSLocalizedFailureReasonErrorKey:errorMessage}];
                    [subscriber sendError:error];
                }
            }];
            return nil;
        }];
    }];
}

@end
